@include('admin.head')
<div class="container">
	<div class="row">
		@include('admin.side')
		<div class="col-md-10" id="content-wrapper">
			<div class="row">
					<div class="col-xs-12">
						<div class="panel panel-default">
						<div class="panel-heading">
							<h3 class="panel-title">添加/编辑技术</h3>
						</div>
						<div class="panel-body">
								<div class="form-horizontal" action="edit_tech/add">
								  <fieldset>
									<div class="form-group row">


								    <div class="form-group">
							      		<label for="select" class="col-xs-3 control-label no_padding_right">添加用户:</label>
								      	<div class="col-xs-4">
								        <select class="form-control input_size" name="user_id">
								          <option value="">请选择</option>
								          @foreach($user as $value)
								          <option value="<%$value['id']%>"><%$value['devicetech']['company_name']%></option>
								          @endforeach
								        </select>
								      </div>
							    	</div>


									<div class="form-group">
									  <label for="inputEmail" class="col-xs-3 control-label no_padding_right">技术名称:</label>
									  <div class="col-xs-4">
									    <input class="form-control input_size" id="inputEmail" placeholder="" name="tech_name" type="text">
									  </div>
									</div>

								    <div class="form-group">
								      <label for="inputEmail" class="col-xs-3 control-label no_padding_right">技术开发者:</label>
								      <div class="col-xs-4">
								        <input class="form-control input_size" id="inputEmail" placeholder="" name="developer" type="text">
								      </div>
								    </div>


								    <div class="form-group">
								      <label for="textArea" class="col-xs-3 control-label no_padding_right">技术优势介绍:</label>
								      <div class="col-xs-4">
								        <textarea class="form-control text_size" rows="3" name="advantage_des" id="textArea"></textarea>
								        <span class="help-block"></span>
								      </div>
								    </div>

								    <div class="form-group">
								      <label for="textArea" class="col-xs-3 control-label no_padding_right">技术原理介绍:</label>
								      <div class="col-xs-4">
								        <textarea class="form-control text_size" rows="3" name="theory_des" id="textArea"></textarea>
								        <span class="help-block"></span>
								      </div>
								    </div>

								    <div class="form-group">
								      <label for="inputEmail" class="col-xs-3 control-label no_padding_right">技术评价:</label>
								      <div class="col-xs-4">
								        <input class="form-control input_size" id="inputEmail" placeholder="" name="score" type="text">
								      </div>
								    </div>

								    <div class="form-group">
								      <label for="inputEmail" class="col-xs-3 control-label no_padding_right">典型客户:</label>
								      <div class="col-xs-4">
								        <input class="form-control input_size" id="inputEmail" placeholder="" name="typical_client" type="text">
								      </div>
								    </div>

								    <div class="form-group">
								      <label for="inputEmail" class="col-xs-3 control-label no_padding_right">技术图片:</label>
								      <div class="col-xs-4">
								            <input type="file" name="imgFile" id="pic">
								            <input type="hidden" class="hiddenName" name="image">
								      </div>
								    </div>

								    <div class="form-group">
							      		<label for="select" class="col-xs-3 control-label no_padding_right">应用技术:</label>
								      	<div class="col-xs-4">
								        <select class="form-control input_size" name="tech_id">
								          <option value="">请选择</option>
								          @foreach($techs as $value)
								          <option value="<%$value['id']%>"><%$value['cate_name']%></option>
								          @endforeach
								        </select>
								      </div>
							    	</div>

							    	<div class="form-group" >
							      		<label for="select" class="col-xs-3 control-label no_padding_right">应用领域:</label>
								      	<div class="col-xs-4">
								        <select class="form-control input_size" name="domain_id" id="domain">
								          <option value="0">请选择</option>
								          @foreach($domains as $value)
								          <option value="<%$value['id']%>"><%$value['cate_name']%></option>
								          @endforeach
								        </select>
								     	</div>
							    	</div>

							    	<div class="form-group" >
							      		<label for="select" class="col-xs-3 control-label no_padding_right"></label>
							     	 	<div class="col-xs-4" id="domains">
							     		</div>
							    	</div>

				        		    <div class="form-group mainBody">
				        		      <label class="col-xs-3 control-label no_padding_right">应用主体:</label>
				        		      <div class="col-xs-4">
				        		        <select class="form-control input_size main_body">
						    	          	<option value="">请选择</option>
						    	          	@foreach ($mBody as $value)
						    	          	<option value="<%$value['id']%>"><%$value['name']%></option>
						    	          	@endforeach
				        		        </select>
				        		      </div>
				        		      <div class="col-xs-3 no_padding_both">
				        		      	<button class="btn btn-primary" id="addMain">添加</button>
				        		      </div>
				        		    </div>

				        		    <div class="form-group">
				        		      <label for="inputEmail" class="col-xs-3 control-label no_padding_right">节能效率:</label>
				        		      <div class="col-xs-4">
				        		        <input class="form-control input_size" name="efficiency" autocomplete="of" id="inputEmail" placeholder="" type="text">
				        		      </div>
				        		    </div>

				        		    <div class="form-group">
				        		      <label for="textArea" class="col-xs-3 control-label no_padding_right">技术描述:</label>
				        		      <div class="col-xs-4">
				        		        <textarea class="form-control text_size" rows="3" name="description" id="textArea"></textarea>
				        		        <span class="help-block"></span>
				        		      </div>
				        		    </div>

					    	    	<div class="form-group">
					    	      		<label for="select" class="col-xs-3 control-label no_padding_right">技术原理概述:</label>
					    	      		<div class="col-xs-6">
											<div id="container" name="theory" style="height:500px;"></div>
						    	    	</div>
					    	    	</div>
							    	<div class="form-group">
								      <div class="col-xs-12 text-center">
								      	<input type="hidden" name="tid">
								        <button type="button" id="sub" class="btn btn-primary ">提交</button>
								      </div>
								    </div>
								  </fieldset>
								</div>
							</div>
						</div>
					</div>
			</div>
		</div>
	</div>
</div>
</div>
@include("admin.footer")
@include("admin.common")
<script type="text/html" id="mainbodyTemp">
<option value="">请选择</option>
{%each list as value}
	<option value="{%value.id}">{%value.name}</option>
{%/each}
</script>

<script type="text/html" id="mainSelectTemp">
<div class="form-group mainBody">
  <label class="col-xs-3 control-label no_padding_right"></label>
  <div class="col-xs-4">
    <select class="form-control input_size main_body">
    </select>
  </div>
  <div class="col-xs-3 no_padding_both">
  	<button class="btn btn-warning deleteMain">删除</button>
  </div>
</div>
</script>

<script type="text/html" id="domain_type">
	<select class="form-control input_size" name="domain_ids">
		{%each list as value index}
		<option value="{%value.id}">{%value.cate_name}</option>
		{%/each}
	</select>
</script>
<script type="text/javascript">
var ue = UE.getEditor('container');
var que = "queue";


var mainBodys;
$.getq(que,"<%url('admin/get_mainbody')%>", function(data){
	if(data.success && data.data.length)
	{
		mainBodys = data.data;
		var str = template("mainbodyTemp", {list : data.data});
		$(".main_body").last().html(str);
	}
})


$("#addMain").bind("click", function(){
	var html = template("mainSelectTemp", {});
	$(html).insertAfter($(".mainBody").last());

	var str = template("mainbodyTemp", {list : mainBodys});
	$(".main_body").last().html(str);
	$(".deleteMain").unbind("click");
	$(".deleteMain").bind("click", function(){
		$(this).parents(".mainBody").remove();
	})
})


$('#pic').AjaxFileUpload({
	path : "<% url('admin/upload') %>",
	onComplete : function(filename){
			$(this).siblings('input[class="hiddenName"]').val(filename.filename);
			$(this).parent(".file").siblings(".fileName").html(filename.filename);
		}
	});


$("#domain").change(function(){
	var parent_id = $(this).val();
	$.getq(que, "<%url('admin/get_domain_type')%>", {parent_id : parent_id}, function(data){
		if(data.success){
			var html = template("domain_type", {list: data.data});
			$("#domains").html(html);
		}
	})

})


$("#sub").click(function(){

	var domain = $("select[name='domain_id']").val();
	var domains = $("select[name='domain_ids']").val();
	if(domains)
	{
		var domain_id = domains;
	}
	else
	{
		var domain_id = domain;
	}
	var user_id = $("select[name='user_id']").val();
	var tech_id = $("select[name='tech_id']").val();
	var tech_name = $("input[name='tech_name']").val().trim();
	var developer = $("input[name='developer']").val().trim();
	var efficiency = $("input[name='efficiency']").val().trim();
	var theory = encodeURIComponent(ue.getContent());
	var advantage_des = $("textarea[name='advantage_des']").val().trim();
	var score = $("input[name='score']").val().trim();
	var description = $("textarea[name='description']").val().trim();
	var typical_client = $("input[name='typical_client']").val().trim();
	var theory_des = $("textarea[name='theory_des']").val().trim();
	var image = $("input[name='image']").val().trim();
	var tid = $("input[name='tid']").val().trim();

	var main_body = [];
	$(".main_body").each(function(k, v){
		if($(v).val() > 0){
			main_body.push($(v).val());
		}
	})


	if (developer == '' || tech_name == '' || advantage_des == '' || score == '' || image == '' || theory == '' || main_body == '' || description == '')
	{
		warning('','必填信息不能为空');
		return false;
	}
	$.post("add_tech/save", {
	tech_name : tech_name,
	developer : developer,
	theory : theory,
	description : description,
	advantage_des : advantage_des,
	theory_des : theory_des,
	score : score,
	image : image,
	efficiency : efficiency,
	typical_client : typical_client,
	domain_id : domain_id,
	tech_id : tech_id,
	tid : tid,
	main_body : main_body,
	user_id : user_id
	},function (data){
		if(data.success){
			success("", "提交成功");
			setTimeout(function(){
				window.location.href="../tech";
			},2000)
		}
		else
		{
			warning("", data.msg);
		}
	})

})
</script>
</html>